<?php 
// En las siguientes lineas guardamos en la variable "final" la ultima parte de la url, lo que servirá para indicar luego en el menu en que seccion estamos.
// In the following lines we save in the variable "final"" the last part of the url, which will indicate later in which section in the menu we are.

$url=$_SERVER['SCRIPT_NAME'];
$last=explode('/', $url); 

for ($i=0;$i<=sizeof($last)-1;$i++) 
{
	if ($i==sizeof($last)-1)
	{
		$final=$last[$i];
	}
}

include ('start.php');;
?>

<h2>INSERT RESULTS</h2><br />

	<?php
	if($_SERVER["REQUEST_METHOD"]=="POST"){

		// Connect and select:
	require("mysqlsettings.php");
	$dbc = mysql_connect($server, $username, $password);
	mysql_set_charset('utf8', $dbc);
	mysql_select_db('db.Regatta', $dbc);

		$regatta=$_POST['regatta'];
		$race=$_POST['race'];
		$position=$_POST['position'];
		$exist=false; // variable de comprobacion de existencia de manga
		

		if($position==0){//si existe la manga que hemos elegigo siguir y si no crear la en la base de datos
			$QueryRace= "SELECT * FROM Race WHERE ID_Regatta = ".$regatta;

			if ($a = mysql_query($QueryRace, $dbc)) { 
				// Retrieve and save every record:
				while ($row = mysql_fetch_array($a)) {

					if ($race==$row['ID_Race']){
						$exist=true;
					}
				}
				if ($exist==false){
					$QueryInsertRace = "INSERT INTO Race (ID_Regatta, ID_Race)
					VALUES ('$regatta', '$race')"; //Insert Race
						// Execute the query:
					if (@mysql_query($QueryInsertRace, $dbc)) {
							print '<p>Manga creada</p>';
					}
				}
			}else { // Query didn't run.
				print '<p style="color: red;">Could not retrieve the data because:<br />'
				.mysql_error($dbc) . '.</p><p>The query being run was: '.$query.'</p>';
			} 
		}

		if($position>=1){ //more than one call POST

			$sail=$_POST['sail'];

			// insert Results
			
			$regatta = mysql_real_escape_string(trim(strip_tags($regatta)), $dbc);
			$race = mysql_real_escape_string(trim(strip_tags($race)), $dbc);
			$position = mysql_real_escape_string(trim(strip_tags($position)), $dbc);
			$sail = mysql_real_escape_string(trim(strip_tags($sail)), $dbc);

				// Define the query of insert:
				$query = "INSERT INTO Results (ID_Regatta, ID_Race, ID_Boat, Position)
				VALUES ('$regatta', '$race', '$sail', '$position')";
		
				// Execute the query:
				if (@mysql_query($query, $dbc)) {
					print '<p>Results introduced</p>';
				} else {
					$position=$position-1;
					print '<p style="color: red;">The result has not been introduced</p>';
				}
		}

		$position=$position+1; // suma 1 a posicion

		//entering results form
		echo'<form action="" method="POST">
			<table>
				<tr>
					<td>Race</td>
					<td>'.$race.'</td>
					<td>for Regatta</td>
					<td>'.$regatta.'</td>
				</tr>
				<tr>
					<td>Position</td>
					<td>'.$position.'</td>
					<td>Number of sail</td>
					<td><input type="text" name="sail"></td>
				</tr>
			</table>
		<input type="submit" value="next">
		<input type="hidden" value="'.$position.'" name="position">
		<input type="hidden" value="'. $regatta.' " name="regatta">
		<input type="hidden" value="'. $race.' " name="race">
		</form>';

		//Query For Resluts
		$QueryResults = "SELECT * FROM Results WHERE ID_Race = "
		.$race." AND ID_Regatta = ".$regatta." ORDER BY Position";

		$ArrayResults=Array(); 

		if ($q = mysql_query($QueryResults, $dbc)) { 

			// Retrieve and save every record:

			while ($row = mysql_fetch_array($q)) {
				$ArrayResults[$row['Position']]=($row['ID_Boat']);
			}
		}else { 
			// Query didn't run.
			print '<p style="color: red;">Could not retrieve the data because:<br />'
			.mysql_error($dbc).'.</p><p>The query being run was: '.$query.'</p>';
		} 

		// Print Results
		Echo "<table id='results'><tr><td>Boat</td><td>Result</td></tr>";
		foreach ($ArrayResults as $key => $value) {
			Echo '<tr><td>'.$value.'</td><td>'.$key.'</tr></td>';
		}
		echo '</table>';

		mysql_close($dbc); // Close the connection.
	}else{
	//Select Regatta

	require("mysqlsettings.php");
	$dbc = mysql_connect($server, $username, $password);
	mysql_set_charset('utf8', $dbc);
	mysql_select_db('db.Regatta', $dbc);


	$QueryRegatta = "SELECT * FROM Regatta";
	$ArrayRegatta=Array();
	
	if ($r = mysql_query($QueryRegatta, $dbc)) { 

	// Retrieve and save every record:

		while ($row = mysql_fetch_array($r)) {
			$ArrayRegatta[$row['ID_Regatta']]=($row['Regatta_Name']);
		}
	}else { // Query didn't run.
		print '<p style="color: red;">Could not retrieve the data because:<br />'
		.mysql_error($dbc).'.</p><p>The query being run was: '.$query.'</p>';
	} // End of query IF.

	mysql_close($dbc); // Close the connection.

	//form regatta and race
		echo"
		<form action='' method='POST'>
			<table id='newresults'>
				<tr>
					<td>Select Regatta</td>
					<td><select id='regatta' name='regatta'>";
						foreach ($ArrayRegatta as $key => $value) {
							echo '<option value="'.$key.'">'.$value.'</option>';
						}
						echo'</select></td>
				</tr>
				<tr>
					<td>Select Race</td>
					<td><input type="text" name="race"></td>
				</tr>
			</table>
			<input type="submit" value="Select">
			<input type="hidden" value="0" name="position"> 
		</form>';
	}
	?>
<?php 
include ('includes/footer.php');
?>